//
//  XMLParser.m
//  kankanVideo
//
//  Created by wangbo on 5/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "XMLParser.h"
#import "CXMLDocument.h"
#import "CXMLElement.h"
#import "IndexData.h"
@implementation XMLParser

+(NSMutableArray *)parseGuiderXML:(NSData *)data{
    NSMutableArray *array=[[NSMutableArray alloc]init];
    CXMLDocument *doc=[[CXMLDocument alloc] initWithData:data options:0 error:nil];
    CXMLElement *root=[doc rootElement];
    for (CXMLElement *element in [root children]) {
        if([element isKindOfClass:[CXMLElement class]]){
            for (int i=0; i<[element childCount]; i++) {
                if([[element childAtIndex:i] isKindOfClass:[CXMLElement class]]){
                    if([[[element childAtIndex:i] name] isEqualToString:@"id"]){
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"titlepic"]){
                        //[array addObject:[[element childAtIndex:i] stringValue]];
                        [array addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }
                }
            }
        }
    }
    [doc release];
    //NSLog(@"guiderxml:%@",array);
    return array;
}

+(NSMutableArray *)parseVideoDetail:(NSData*)data{
    NSMutableArray *array=[[NSMutableArray alloc]init];
    CXMLDocument *doc=[[CXMLDocument alloc] initWithData:data options:0 error:nil];
    CXMLElement *root=[doc rootElement];
    for (CXMLElement *element in [root children]) {
        if([element isKindOfClass:[CXMLElement class]]){
            NSMutableArray *arr=[NSMutableArray arrayWithCapacity:4];
            for (int i=0; i<[element childCount]; i++) {
                if([[element childAtIndex:i] isKindOfClass:[CXMLElement class]]){
                    if([[[element childAtIndex:i] name] isEqualToString:@"videourl"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                        [array addObject:arr];
                    }/*else if([[[element childAtIndex:i] name] isEqualToString:@"titleurl"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"titlepic"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"pubDate"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                        [array addObject:arr];
                    }*/
                }
            }
        }
    }
    //NSLog(@"videolist:%@",array);
    return array;
}
+(NSMutableArray *)parseVideoList:(NSData *)data{
    NSMutableArray *array=[[NSMutableArray alloc]init];
    CXMLDocument *doc=[[CXMLDocument alloc] initWithData:data options:0 error:nil];
    CXMLElement *root=[doc rootElement];
    for (CXMLElement *element in [root children]) {
        if([element isKindOfClass:[CXMLElement class]]){
            NSMutableArray *arr=[NSMutableArray arrayWithCapacity:4];
            for (int i=0; i<[element childCount]; i++) {
                if([[element childAtIndex:i] isKindOfClass:[CXMLElement class]]){
                    if([[[element childAtIndex:i] name] isEqualToString:@"title"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"titleurl"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"titlepic"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                        [array addObject:arr];
                    }
                }
            }
        }
    }
    //NSLog(@"videolist:%@",array);
    return array;
}

+(NSMutableArray *)parseListXML:(NSData *)data{
    NSMutableArray *array=[[NSMutableArray alloc]init];
    return array;
}

+(NSMutableArray *)parseChannelList:(NSData *)data{
    NSMutableArray *array=[[NSMutableArray alloc]init];
    CXMLDocument *doc=[[CXMLDocument alloc] initWithData:data options:0 error:nil];
    CXMLElement *root=[doc rootElement];
    for (CXMLElement *element in [root children]) {
        if([element isKindOfClass:[CXMLElement class]]){
            NSMutableArray *arr=[NSMutableArray arrayWithCapacity:3];
            for (int i=0; i<[element childCount]; i++) {
                if([[element childAtIndex:i] isKindOfClass:[CXMLElement class]]){
                    if([[[element childAtIndex:i] name] isEqualToString:@"title"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"titlepic"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"classid"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                        [array addObject:arr];
                    }
                }
            }
        }
    }//weburl=[weburl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    //NSLog(@"channellist%@",array);
    return array;
}

+(NSMutableArray *)parseCategoryList:(NSData *)data{
    NSMutableArray *array=[[NSMutableArray alloc]init];
    CXMLDocument *doc=[[CXMLDocument alloc] initWithData:data options:0 error:nil];
    CXMLElement *root=[doc rootElement];
    for (CXMLElement *element in [root children]) {
        if([element isKindOfClass:[CXMLElement class]]){
            NSMutableArray *arr=[NSMutableArray arrayWithCapacity:3];
            for (int i=0; i<[element childCount]; i++) {
                if([[element childAtIndex:i] isKindOfClass:[CXMLElement class]]){
                    if([[[element childAtIndex:i] name] isEqualToString:@"title"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"source"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"bigpic"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"smallpic"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"pubDate"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"classid"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"pname"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"isgood"]){
                        static BOOL fg;
                        if (!fg) {
                            [arr addObject:@"1"];
                            fg = YES;
                            //aaaa
                        }
                        else [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                    }else if([[[element childAtIndex:i] name] isEqualToString:@"firsttitle"]){
                        [arr addObject:[[[element childAtIndex:i] stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
                        [array addObject:arr];
                    }
                }
            }
        }
    }
    return array;
}
@end
